﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Telnet UI Application
 * Version 2.10
 * 
 * Connection Definition Collection Class
 *      Provides a custom collection class for storing ConnectionDefinition instances.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.IO;
using System.Net;
using Adaptive.Foundation;
using Adaptive.Network;
using Adaptive.Network.Sockets;

namespace Adaptive.Telnet.UI
{
    /// <summary>
    /// Provides a custom collection class for storing <see cref="ConnectionDefinition"/> instances.
    /// </summary>
    public sealed class ConnectionDefinitionCollection : GenericCollectionBase<ConnectionDefinition>
    {
        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ConnectionDefinitionCollection()
            : base()
        {
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Gets the unique key to associate with the object when stored in the collection.
        /// </summary>
        /// <param name="itemToGetKeyFor">
        /// The object being appended to the collection intance.
        /// </param>
        /// <returns>
        /// The reference key value for the instance.
        /// </returns>
        protected override string GetKeyForObject(ConnectionDefinition itemToGetKeyFor)
        {
            if (itemToGetKeyFor == null)
                return string.Empty;
            else
                return itemToGetKeyFor.Name;
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Public Methods / Event Methods
        /// <summary>
        /// Loads the content of the collection from the specified stream.
        /// </summary>
        /// <param name="inputStream">
        /// An open <see cref="Stream"/> instance to be read from.
        /// </param>
        public void Load(Stream inputStream)
        {
            BinaryReader reader = null;             //Data reader.
            int count = 0;                          //Iteration counter.
            int length = 0;                         //Iteration limit.
            ConnectionDefinition newItem = null;    //New item(s) to add to collection.

            try
            {
                reader = new BinaryReader(inputStream);
            }
            catch
            {
                reader = null;
            }
            if (reader != null)
            {
                //Clear current contents.
                Clear();

                //Read the number of items to load.
                length = reader.ReadInt32();
                for (count = 0; count < length; count++)
                {
                    newItem = new ConnectionDefinition();
                    newItem.Load(inputStream);
                    Add(newItem);
                }
                reader.Close();
            }
            reader = null;
        }
        /// <summary>
        /// Saves the current content to the output stream.
        /// </summary>
        /// <param name="outputStream">
        /// An open output stream instance.
        /// </param>
        public void Save(Stream outputStream)
        {
            BinaryWriter writer = null;

            //Write the number of items.
            try
            {
            writer = new BinaryWriter(outputStream);
            }
            catch
            {
                writer = null;
            }
            if (writer != null)
            {
                writer.Write(Count);
                MoveFirst();
                while (!Eof)
                {
                    Value.Save(outputStream);
                    MoveNext();
                }
                writer.Close();
            }
            writer = null;
        }
        #endregion
    }
}